const app = getApp();
Page({
  data: {
    monologue: '',
    perviewPic:'',
    // imgList:[{
    //   'src':'',
    // }],
    imgList:[],
    picServer: app.globalData.picServer,
  },
  onLoad: function (options) {
    // this.initPage();
  },
  initPage: function () {
    //刷新用户信息
    var _this=this;
    app.getUserInfo({"openId":wx.getStorageSync('openid')});
    var userInfo = wx.getStorageSync('userInfo');
    this.setData({
      monologue: userInfo.member.monologue
    });
  },
  doSave: function () {
    var _this=this;
    var list=_this.data.imgList;
    var size =_this.data.imgList.length;
    app.doReq(app.globalData.SERVICE.INDEX.MOODPUBLIC, {
      "id": app.globalData.userid,
      'dynamic.content': _this.data.monologue,
      'dynamic.url1': size>0?list[0]:'',
      'dynamic.url2': size>=2?list[1]:'',
      'dynamic.url3': size >= 3 ? list[2] : '',
      'dynamic.url4': size >= 4 ? list[3] : '',
      'dynamic.url5': size >= 5 ? list[4] : '',
      'dynamic.url6': size >= 6 ? list[5] : '',
      'dynamic.url7': size >= 7 ? list[6] : '',
      'dynamic.url8': size >= 8 ? list[7] : '',
      'dynamic.url9': size >=9 ? list[8] : ''
    }, 'get',function (res) {
      var data=res.data
      if (data.code == 200) {
       wx.showToast({
         title: '发表成功',
         icon:"success",
         success:function(){
           var pages = getCurrentPages();
           var currentPage = pages[pages.length - 1];//当前页面
           var pervPagge = pages[pages.length - 2];//上个页面
           pervPagge.setData({
              reloadFlag:true,
           })
           wx.navigateBack({});
         }
       });
      }
    });
  },
  whenInput: function (e) {
    var context = e.detail.value;
    this.data.monologue = context;
  },
  whenFocus: function (e) {
    this.setData({
      monologue: ''
    });
  },
  addPhoto:function(){
    var _this=this;
    var list=this.data.imgList;
    if(list.length<=8){
      wx.chooseImage({
        count: 1,
        success: function (res) {
          var tempFilePaths = res.tempFilePaths;//文件临时访问路径
          var tempFiles = res.tempFiles;//文件临时存储对象
          wx.uploadFile({
            // url: app.globalData.SERVICE.USERCENTER.IMGUPLOADDO,
            url: app.globalData.https_url+'saveImgAsBase64',//将图片转换为base64
            // url:  'http://localhost:8080/qql/saveImgAsBase64',//将图片转换为base64
            filePath: tempFilePaths[0],
            name: 'imgPath',
            success: function (res) {
              var data = res.data;
              if (typeof (data) == 'string') {
                data = JSON.parse(data);
              }
              app.doReq(app.globalData.SERVICE.USERCENTER.IMGUPLOADDO, { imgStr: data.base64Content }, 'post', function (res) {
                var data = res.data;
                var url = data.url;
                _this.setData({
                  imgList: _this.data.imgList.concat(url)
                })
              })
            }
          })
        },
      })
    }else{
      wx.showToast({
        title: '最多只能上传9张照片！',
        icon:"none"
      })
    }
  },
  del:function(e){
    var index = e.currentTarget.dataset.index;
    var list = this.data.imgList;
     list.splice(index, 1)
  console.log(list);
    this.setData({
      imgList: list
    })
  }
})